﻿@{
	Layout = null;
}
<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
<link href="~/lib/layui/css/layui.mobile.css" rel="stylesheet" />
<style>
	.layui-form-label {
		width: 160px
	}

	.layui-input-block {
		margin-left: 200px;
		width: 186px;
	}

	.layui-form-item {
		margin-bottom: 30px; /* 每个行之间的距离 */
		width: 400px
	}

</style>
<form class="layui-form" action="">
	<table>
		<tr>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">客户编码</label>
					<div class="layui-input-block">
						<input type="text" name="CustomerCode" lay-verify="required" placeholder="请输入客户编码" autocomplete="off" class="layui-input">
						<input type="checkbox" name="autoGenerate" lay-skin="switch" lay-filter="switchTest">自动生成
					</div>

				</div>
			</td>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">客户名称</label>
					<div class="layui-input-block">
						<input type="text" name="CustomerName" lay-verify="required" placeholder="请输入客户名称" autocomplete="off" class="layui-input">
					</div>
				</div>
			</td>
		</tr>
		<tr>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">客户简称</label>
					<div class="layui-input-block">
						<input type="text" name="CustomerAbbreviation" lay-verify="required" placeholder="请输入客户简称" autocomplete="off" class="layui-input">
					</div>
				</div>
			</td>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">客户类型</label>
					<div class="layui-input-inline">
						@* 下拉框默认值选择：selected *@
						<select name="CustomerType" lay-filter="aihao">
							<option value=""></option>
							<option value="1">企业</option>
							<option value="2">个人</option>
						</select>
					</div>
				</div>
			</td>
		</tr>
		<tr>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">客户电话</label>
					<div class="layui-input-block">
						<input type="text" name="CustomerPhone" lay-verify="required" placeholder="请输入客户电话" autocomplete="off" class="layui-input">
					</div>
				</div>
			</td>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">联系人</label>
					<div class="layui-input-block">
						<input type="text" name="ContactName" lay-verify="required" placeholder="请输入联系人" autocomplete="off" class="layui-input">
					</div>
				</div>
			</td>
		</tr>
		<tr>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">联系人电话</label>
					<div class="layui-input-block">
						<input type="text" name="ContactPhone" lay-verify="required" placeholder="请输入联系人电话" autocomplete="off" class="layui-input">
					</div>
				</div>
			</td>
			<td>
				<div class="layui-form-item">
					<label class="layui-form-label">是否启用</label>
					<div class="layui-input-block">
						<input type="radio" name="State" value="true" title="启用" checked>
						<input type="radio" name="State" value="false" title="禁用">
					</div>
				</div>
			</td>
		</tr>
	</table>
	<table>
		<div style="margin-left:400px;margin-top:30px">
			<button type="submit" class="layui-btn" lay-submit lay-filter="add">立即提交</button>

			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		</div>
	</table>
</form>

<script src="~/lib/layui/layui.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script>
	layui.use(['form', 'upload', 'layer', 'element'], function () {
		var form = layui.form;
		var layer = layui.layer;
		var upload = layui.upload;
		var element = layui.element;

		// 监听复选框变化，自动生成客户编码
		form.on('switch(switchTest)', function (data) {
			if (data.elem.checked) {
				// 生成随机字母+数字的编码并设置到CustomerCode输入框中
				var randomNum = Math.floor(Math.random() * 10) + '';
				for (var i = 0; i < 4; i++) {
					randomNum += Math.floor(Math.random() * 10);
				}
				randomNum = 'C00' + randomNum;
				$('input[name="CustomerCode"]').val(randomNum);
			}
			else
			{
				$('input[name="CustomerCode"]').val('');
			}
		});


		// 提交事件
		form.on('submit(add)', function (data) {
			var field = data.field; // 获取表单字段值
			$.ajax({
				url: "/CustomerManagement/PostCustomerManagementInfo",
				type: "post",
				async: false,
				data: field,
				datatype: "json",
				success: function (res) {
					console.log(res.data)
					if (res.code > 0) {
						layer.alert('添加成功', { title: '操作结果' }, function () {
							var index = parent.layer.getFrameIndex(window.name); // 获取当前 iframe 层的索引
							parent.layer.close(index); // 关闭当前 iframe 层
							parent.location.reload(); // 刷新父页面

						});
					}
				}
			});
			return false; // 阻止默认 form 跳转
		});
	})
</script>